var treeObj;
var chartEC;

$(function() {
	tree_init();
	checkboxs_init();
	chartdata_init();
	$("#chart-select").change(function() {
		if($("#chartStyle-select").children('option:selected').val() == "stack"){
		 chartStyleChange();
		}
		
		isChartButtonShow();
	});
	$("#chartStyle-select").change(function() {
		chartStyleChange();
		isChartButtonShow();
	});
});

// tree初始化
var tree_init = function() {
	// 获取油气井数据
	$.ajax({
		url : './tree/tree',
		dataType : 'json'
	}).done(function(data) {
		if (!data) {
			return;
		}

		// 初始化树
		var settings = {
			treeId : "mytree",
			check : {
				enable : true,
				chkboxType : {
					"Y" : "",
					"N" : "s"
				}
			},
			view : {
				showIcon : false,
				fontCss : {
					font_size : "13px"
				}
			},
			callback : {
				onClick : myOnClick,
				onCheck : myOnCheck
			}
		};

		treeObj = $.fn.zTree.init($('#xzqtree'), settings, data);
		// var nodes = treeObj.getNodesByParam("name", "root", null);
		// var treeId=treeObj.
		var nodes = treeObj.getNodes();
		if (nodes.length > 0) {
			treeObj.expandNode(nodes[0], true, false, true);
		}
		var nodes2 = treeObj.getNodesByParam("parentId", "-1", null);

		if (nodes2.length == 2) {
			treeObj.expandNode(nodes2[0], true, false, true);
			treeObj.expandNode(nodes2[1], true, false, true);
		}
		var nodes2 = treeObj.getNodesByParam("name", "谭坪外围", null);
		if (nodes2.length == 1) {
			treeObj.expandNode(nodes2[0], true, false, true);
		}

		// 节点点击的处理
		function myOnClick(event, treeId, treeNode) {
			// alert(treeNode.tId + ", " + treeNode.name);

			if (treeNode.chkDisabled == false) {

				if (treeNode.checked == true) {
					treeNode.checked = false;
				} else {
					treeNode.checked = true;
				}

				treeObj.updateNode(treeNode);
				isChartButtonShow();
			}

		}
		;
		// 节点checked的处理
		function myOnCheck(event, treeId, treeNode) {
			// alert(treeNode.tId + ", " + treeNode.name + "," +
			// treeNode.checked);
			isChartButtonShow();
		}
		;
		chartStyleChange();

	});
}

var checkboxs_init = function() {

	// 初始化checkboxs数据
	$
			.ajax({
				url : './tree/checkbox',
				dataType : 'json'
			})
			.done(
					function(data) {
						var tableString = "";

						$
								.each(
										data,
										function(i, value) {

											if (i % 2 == 0) {

												tableString += "<tr><td><input class='chart-checkbox' type='"
														+ "checkbox'"
														+ "name='' id='"
														+ value.code
														+ "-check'"
														+ "value='"
														+ value.code
														+ "' /><label for='"
														+ value.code
														+ "-check'>"
														+ value.name + "</td>";
											} else {
												tableString += "<td><input class='chart-checkbox' type='"
														+ "checkbox'"
														+ "name='' id='"
														+ value.code
														+ "-check'"
														+ "value='"
														+ value.code
														+ "' /><label for='"
														+ value.code
														+ "-check'>"
														+ value.name
														+ "</td></tr>";
											}

										});
						$('#left-property-table').append(tableString);
						chartStyleChange();
					});
}

var isChartButtonShow = function() {
	var showButton = false;
	var level = $("#chart-select").children('option:selected').val();
	
	if (treeObj != null) {
		if (level != -1) {
			var nodes = treeObj.getCheckedNodes(true);
			// alert("length："+nodes.length);
			for (var i = 0; i < nodes.length; i++) {
				if (nodes[i].level == level) {
					showButton = true;
					break;
				}
			}

			if (showButton) {
				$("#showChartBtn").removeAttr("disabled");// 要变成Enable，JQuery只能这么写
			} else {
				$("#showChartBtn").attr("disabled", "disabled");// 再改成disabled
			}

		} else {
			var nodes = treeObj.getNodes();
			var nodes_array = treeObj.transformToArray(nodes);
			for (var i = 0; i < nodes_array.length; i++) {
				treeObj.checkNode(nodes_array[i], false, true);
				treeObj.setChkDisabled(nodes_array[i], true);
			}
		}
	}
}

var chartStyleChange = function() {

	var style = $("#chartStyle-select").children('option:selected').val();

	if (style == "stack") {
		var level = $("#chart-select").children('option:selected').val();
		if (level != -1) {
			// 设置属性checkbox 除了日产气其他都不可选
			$(".chart-checkbox").attr("checked", false);
			$("#QQ-check").prop("checked", true);
			$(".chart-checkbox").attr("disabled", "disabled");// 改成disabled

			// 设置作图类型checkbox 除了井组产气曲线其他都不可选
			$("#chart-select").empty();
			$("#chart-select").append(
					"<option value='-1'>按投产时间叠合图</option>"
							+ "<option value='2'>按井组叠合图</option>");
			$("#chart-select").find("option[value='2']").prop("selected", true);
			$("#chart-select").removeAttr("disabled");// 要变成Enable，JQuery只能这么写

			// 设置树结构中除了井组层其他都不可选
			if (treeObj != null) {
				var nodes = treeObj.getNodes();
				var nodes_array = treeObj.transformToArray(nodes);
				for (var i = 0; i < nodes_array.length; i++) {
					if (nodes_array[i].level != 2) {
						treeObj.setChkDisabled(nodes_array[i], true);
					} else {	
						treeObj.setChkDisabled(nodes_array[i], false);
						treeObj.expandNode(nodes_array[i], false, true, true);
						treeObj.checkNode(nodes_array[i], true, true);
					}
				}

			}
		} else {
			var nodes = treeObj.getNodes();
			var nodes_array = treeObj.transformToArray(nodes);
			for (var i = 0; i < nodes_array.length; i++) {
				treeObj.checkNode(nodes_array[i], false, true);
				treeObj.setChkDisabled(nodes_array[i], true);
			}	
		}

	} else {
		
		$(".chart-checkbox").attr("checked", false);
		$(".chart-checkbox").removeAttr("disabled");// 要变成Enable，JQuery只能这么写

		$("#chart-select").empty();
		$("#chart-select").append(
				"<option value='3'>单井产气曲线</option>"
						+ "<option value='2'>井组产气曲线</option>"
						+ "<option value='1'>区块产气曲线</option>"
						+ "<option value='0'>气田总产气曲线</option>");
		$("#chart-select").find("option[value='3']").prop("selected", true);
		$("#chart-select").removeAttr("disabled");// 要变成Enable，JQuery只能这么写

		// 设置树结构中除了井组层其他都不可选
		if (treeObj != null) {

			var nodes = treeObj.getNodes();
			var nodes_array = treeObj.transformToArray(nodes);
			for (var i = 0; i < nodes_array.length; i++) {
				var nodes2 = treeObj.getNodesByParam("name", "谭坪外围", null);
				if (nodes2.length == 1) {
					treeObj.expandNode(nodes2[0], true, false, true);
				}
				nodes_array[i].checked = false;
				treeObj.setChkDisabled(nodes_array[i], false);
			}

		}
	}
}
